package live.yanxiaohui.deadletter.procuder;

import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description todo
 * @CSDN https://blog.csdn.net/yxh13521338301
 * @Author： yanxh<br>
 * @Date 2020-06-28 17:31<br>
 * @Version 1.0<br>
 */
@RestController
@RequestMapping("/dead/letter/")
public class DeadLetterProducer {

    @Autowired
    private AmqpTemplate amqpTemplate;


    /**
     *
     * @param msg 消息内容
     * @param key 路由key
     * @param exchange 交换机名称
     * @return
     * @throws Exception
     */
    @GetMapping("/send")
    public String send(String msg, String key, String exchange) throws Exception{
        if(StringUtils.isBlank(msg)){
            return "消息为空";
        }

        // 队列名称 路由key 消息内容
        amqpTemplate.convertAndSend(exchange, key, msg, (message)->{
            // 消息有效期10s
            message.getMessageProperties().setExpiration("10000");
            return message;
        });
        return "消息生成成功";
    }
}
